/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import newkfp.StockBean;
import newkfp.StockFacade;
import newkfp.util.JsfUtil;
import newkfp.util.PaginationHelper;

/**
 *
 * @author A7med Kotb
 */
@ManagedBean(name = "stockController")
@SessionScoped
public class StockController implements Serializable {

    private StockBean current;
    private DataModel items = null;
    @EJB
    private newkfp.StockFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;
    private Date fromDate;
    private Date toDate;

    private StockFacade getFacade() {
        return ejbFacade;
    }

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }

    public String getProjectReport() {
        return prepareList();
    }

    public StockController() {
    }

    public StockBean getSelected() {
        if (current == null) {
            current = new StockBean();
            selectedItemIndex = -1;
        }
        return current;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getListOfStocks().size();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getListOfStocks());
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "stockReport";
    }

    public String prepareView() {
        current = (StockBean) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        return "List";
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    public List<StockBean> getListOfStocks() {
        List<StockBean> stockList = new ArrayList<StockBean>();
        if (fromDate != null && toDate != null) {
            stockList = getFacade().getReport(fromDate, toDate);
            return stockList;
        } else {
            return stockList;
        }
    }
}
